package com.by.bean.params;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Data
public class page {
    @ApiModelProperty("当前页默认第1页")
    int page;

    @ApiModelProperty(value = "每页记录数默认10条",required = false)
    int pageSize;


    /**
     * 将javabean转换为map
     *
     * @return
     */
    public Map toMap() {
        //先转换父类
        Map parentMap = convertToMap(this.getClass().getSuperclass());
        //获取当前类
        Map childMap = convertToMap(this.getClass());
        parentMap.putAll(childMap);
        return parentMap;
    }

    /**
     * 获取当前类的所有字段  并转换为map
     *
     * @param clazz
     * @return
     */
    Map convertToMap(Class clazz) {
        Map map = new HashMap();
        //利用反射获取当前类的所有字段
        Field[] fields = clazz.getDeclaredFields();
        //遍历
        for (Field f : fields) {
            //获取字段名称
            String key = f.getName();
            //获取该字段的值
            Object value = null;
            try {
                value = f.get(this);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            map.put(key, value);
        }
        return map;
    }


}
